Skip to content

Comments

Refactor OpenSearch model into builder services#945

Merged
jazairi merged 2 commits intomainfrom
use-372
Feb 20, 2026
Merged

Refactor OpenSearch model into builder services#945
jazairi merged 2 commits intomainfrom
use-372

Conversation

@jazairi
Copy link
Contributor

@jazairi jazairi commented Feb 19, 2026

Why these changes are being introduced:

The OpenSearch model has become unwieldy, and will continue to grow as we add semantic search
functionality.

Relevant ticket(s):

How this addresses that need:

This breaks out the OpenSearch logic into multiple builder models: Filter Builder and Lexical Query
Builder (with Semantic Query Builder to come
later). This refactor is intended to improve
maintainability.

Side effects of this change:

  • The OpenSearch model still retains some query building logic, most notably search highlights.
    We could consider creating a very small
    HighlightBuilder service if we want to commit to
    this architectural pattern.
  • As discussed in the ADR for this change, we will not be implementing the 'Strategy' selector model
    at this time. That change will come later, once
    semantic search has been implemented.

Developer

  • All new ENV is documented in README
  • All new ENV has been added to Heroku Pipeline, Staging and Prod
  • ANDI or Wave has been run in accordance to
    our guide and
    all issues introduced by these changes have been resolved or opened as new
    issues (link to those issues in the Pull Request details above)
  • Stakeholder approval has been confirmed (or is not needed)

Code Reviewer

  • The commit message is clear and follows our guidelines
    (not just this pull request message)
  • There are appropriate tests covering any new functionality
  • The documentation has been updated or is unnecessary
  • The changes have been verified
  • New dependencies are appropriate or there were no changes

Requires database migrations?

NO

Includes new or updated dependencies?

NO

Why these changes are being introduced:

The OpenSearch model has become unwieldy, and will
continue to grow as we add semantic search
functionality.

Relevant ticket(s):

- [USE-372](https://mitlibraries.atlassian.net/browse/USE-372)

How this addresses that need:

This breaks out the OpenSearch logic into multiple
builder models: Filter Builder and Lexical Query
Builder (with Semantic Query Builder to come
later). This refactor is intended to improve
maintainability.

Side effects of this change:

- The OpenSearch model still retains some query
building logic, most notably search highlights.
We could consider creating a very small
HighlightBuilder service if we want to commit to
this architectural pattern.
- As discussed in the ADR for this change, we will
not be implementing the 'Strategy' selector model
at this time. That change will come later, once
semantic search has been implemented.
@mitlib mitlib temporarily deployed to timdex-api-p-use-372-bgmngqsi7 February 19, 2026 16:21 Inactive
@JPrevost JPrevost self-assigned this Feb 19, 2026
Copy link
Member

@JPrevost JPrevost left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works and is a huge improvement. I'm commenting instead of approving in case you want to address any of the non-blocking comments I made. If you prefer as-is just let me know and I'm happy to approve.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking.

While we are refactoring, I think it would be nice to move calculate_size to its own method. I'm okay with it staying in the opensearch class, but pulling this to a method would help make build_query easier to read.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great call!

@query_strategy.build(@params, @fulltext)
end

def sort
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking.

The ADR notes extracting sort to its own Builder even though it is very simple at this time. If it was an intentional decision to leave it here, I'm totally fine with seeing how this feels before changing it, but if it was just overlooked I do slightly prefer extraction.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not intentional at all. I can extract it before we merge.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably want to think about whether to extract source and highlight to follow the same patterns for the entire query building. They are slightly different as they are conditionally included in the hash. I'm okay with extracting or waiting to see how this class feels after this work lands to see if it warrants change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I considered a HighlightBuilder class, and source is a good candidate, too. Given that we're extracting source, it makes sense to do all three as part of this change.

- Move highlights, sorting, and source to separate models

- Refactor size calculation into method
@jazairi jazairi requested a review from JPrevost February 20, 2026 17:08
@jazairi jazairi temporarily deployed to timdex-api-p-use-372-bgmngqsi7 February 20, 2026 17:08 Inactive
@jazairi jazairi merged commit 041d0a1 into main Feb 20, 2026
2 checks passed
@jazairi jazairi deleted the use-372 branch February 20, 2026 18:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants